package main

import (
	"fmt"
	"log"

	"github.com/louloulin/dataflare/pkg/workflow/processor"
)

func main() {
	// Create transform processor
	fmt.Println("Creating transform processor...")
	transformProcessor, err := processor.NewProcessor("transform")
	if err != nil {
		log.Fatalf("Failed to create transform processor: %v", err)
	}

	// Initialize processor with transform script
	err = transformProcessor.Init(map[string]interface{}{
		"function": `
			function process(data) {
				// Parse the data
				const obj = JSON.parse(data);
				
				// Double the value
				obj.value = obj.value * 2;
				
				// Add processed flag
				obj.processed = true;
				
				// Return the processed data
				return JSON.stringify(obj);
			}
		`,
	})
	if err != nil {
		log.Fatalf("Failed to initialize transform processor: %v", err)
	}

	// Test data
	testData := []string{
		`{"id": 1, "value": 10}`,
		`{"id": 2, "value": 20}`,
		`{"id": 3, "value": 30}`,
	}

	// Process data
	fmt.Println("\nProcessing data:")
	for i, data := range testData {
		// Process data
		processedData, err := transformProcessor.Process([]byte(data), nil)
		if err != nil {
			log.Fatalf("Failed to process data: %v", err)
		}

		fmt.Printf("Record %d:\n", i+1)
		fmt.Printf("  Original: %s\n", data)
		fmt.Printf("  Processed: %s\n", string(processedData))
	}

	// Close processor
	err = transformProcessor.Close()
	if err != nil {
		log.Fatalf("Failed to close processor: %v", err)
	}

	fmt.Println("\nTransform processor test completed successfully!")
}
